# (c) 2010 Michael Penhallegon
# All Rights Reserved

from django.db import models
from django.contrib.auth.models import User
from datetime import datetime

# Create your models here.
class Theme(models.Model):
    name = models.CharField(max_length=40)
    url = models.CharField(max_length=200)
    def __unicode__(self):
        return self.name
class Page(models.Model):
    (EDITING,PUBLIC,PRIVATE) = (1,3,2)
    APPROVAL_CHOICES = ((EDITING,'Editing'),
                        (PRIVATE,'Private'),
                        (PUBLIC,'Public'),
                        )

    name = models.CharField(max_length=20)
    link = models.SlugField()
    themes = models.ManyToManyField(Theme)
    approved = models.PositiveSmallIntegerField(choices=APPROVAL_CHOICES, default=EDITING)
    def __unicode__(self):
        return self.name
class PublicArticleManager(models.Manager):
    def get_query_set(self):
        return super(PublicArticleManager,self).get_query_set().filter(status=3).order_by('-pub_date')
class Article(models.Model):
    CLOSED_STATUS = 1
    EDITING_STATUS = 2
    LIVE_STATUS = 3
    STATUS_CHOICES = (
        (CLOSED_STATUS,'Closed'),
        (EDITING_STATUS,'Editing'),
        (LIVE_STATUS,'Public'),
        )
    author = models.ManyToManyField(User)
    page = models.ForeignKey(Page)
    title = models.CharField(max_length=140)
    slug = models.SlugField(max_length=140)
    body = models.TextField()
    create_date = models.DateTimeField(auto_now=True)
    pub_date = models.DateTimeField(blank=True)
    status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, default=EDITING_STATUS)

    objects = models.Manager()
    pub_objects = PublicArticleManager()
    def __unicode__(self):
        return self.title

